package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/*分类管理Service实现类*/
@Service
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    // 注入Mapper层Bean对象
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public void save(CategoryDTO categoryDTO) {
        // 将前端的DTO对象转换成数据库表实体类对象
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        /*
        // 设置基本属性
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        category.setCreateUser(BaseContext.getCurrentId());
        category.setUpdateUser(BaseContext.getCurrentId());
        */
        // 设置默认状态为禁用
        category.setStatus(StatusConstant.DISABLE);
        // 调用Mapper层插入方法
        categoryMapper.insert(category);
    }

    @Override
    public PageResult pageQuery(CategoryPageQueryDTO categoryPageQueryDTO) {
        // 获取分页查询参数
        int pageNumber = categoryPageQueryDTO.getPage();
        int pageSize = categoryPageQueryDTO.getPageSize();
        // 调用PageHelper插件
        /* = PageHelper.startPage(pageNumber, pageSize);*/
        // 调用Mapper层查询数据库
        Page<Category> page = categoryMapper.pageQuery(categoryPageQueryDTO);

        // 获取page对象中的查询结果记录数,和结果
        long total = page.getTotal();
        List<Category> categoryList = page.getResult();
        PageResult pageResult = new PageResult();
        pageResult.setTotal(total);
        pageResult.setRecords(categoryList);
        // 返回pageResult对象
        return pageResult;
    }

    @Override
    public void deleteById(Long id) {
        // 删除分类前,先行判断分类下是否菜品
        Integer countOfDish = dishMapper.countByCategoryId(id);
        // 判断菜品数量食
        if (countOfDish > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_DISH); //　抛出菜品不为空异常　
        }
        // 判断关联套餐数量是否为０
        Integer countOfSet = setmealMapper.countByCategoryId(id);
        if (countOfSet > 0) {
            throw new DeletionNotAllowedException(MessageConstant.CATEGORY_BE_RELATED_BY_SETMEAL); //　抛出套餐不为空异常　
        }
        // 调用mapper层接口
        categoryMapper.deteleById(id);
    }

    @Override
    public void update(CategoryDTO categoryDTO) {
        // 属性复制到实体类对象中
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO, category);
        /*
        // 设置基本属性
        category.setUpdateUser(BaseContext.getCurrentId()); // 操作人id
        category.setUpdateTime(LocalDateTime.now()); // 当前时间
        */
        // 调用Mapper层修改方法
        categoryMapper.update(category);
    }

    @Override
    public void enableOrDisable(Integer status, Long id) {
        /*修改分类状态*/
        Category category = new Category();
        category.setId(id);
        category.setStatus(status);
        /*
        category.setUpdateTime(LocalDateTime.now());
        category.setUpdateUser(BaseContext.getCurrentId());
        */
        // 调用mapper层方法
        categoryMapper.update(category);
    }

    @Override
    public List<Category> list(Integer type) {
        // 调用mapper层接口，根据type类型查询分类
        return categoryMapper.list(type);
    }
}
